package trancation;

import javax.sql.DataSource;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jmx.export.UnableToRegisterMBeanException;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:mysql/spring-config.xml")
@TransactionConfiguration(transactionManager = "txManager")

public class TimeoutTest {
	@Autowired
	private DataSource ds;

	// @Test
	public void testTimeout() throws InterruptedException {
		Thread.sleep(2500L);
		long t1 = System.currentTimeMillis();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
		jdbcTemplate.execute("update user set name='1243'");
		System.out.println(System.currentTimeMillis() - t1);

	}

	// @Test
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
	public void testRollBackInException() {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
		User user = new User();
		jdbcTemplate.update("update user set name='oo' where age=2");
		jdbcTemplate.update("update user set name=? where age=?", "wang", 3);
		// �ع�
		String test = null;
		if (test.equals("")) {
			int i = 1;
		}

	}

	@Test
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void testRollBackInException2() {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
		User user = new User();
		jdbcTemplate.update("update user set name='oo' where age=2");
		jdbcTemplate.update("update user set name=? where age=?", "wang", 3);
		// testForException();
	}

	public void testForException() {
		throw new UnableToRegisterMBeanException("abc");
	}
}
